package chatroom.location;

import java.math.BigDecimal;

import chatroom.domain.User;

public class Distance {
	
	public static BigDecimal calculateDistance(User user1, User user2){
		BigDecimal multiplyLat = new BigDecimal(10000/90);
		BigDecimal lat1 = user1.getLatitude().multiply(multiplyLat);
		BigDecimal lat2 = user2.getLatitude().multiply(multiplyLat);
		
		BigDecimal multiplyLong = new BigDecimal(40000/360);
		BigDecimal long1 = user1.getLongitude().multiply(multiplyLong);
		BigDecimal long2 = user2.getLongitude().multiply(multiplyLong);
		BigDecimal diffLatitude = lat1.subtract(lat2); 
		BigDecimal diffLongitude = long1.subtract(long2); 
		
		BigDecimal temp = diffLatitude.pow(2).add(diffLongitude.pow(2));
		System.out.println(temp);
		return new BigDecimal(Math.sqrt((temp).doubleValue())); 
	}
	
	public static double calculate2(User user1, User user2){
		int R = 6371;
		double dLat = Math.toRadians((user2.getLatitude().subtract(user1.getLatitude()).doubleValue()));
		double dLon = Math.toRadians((user2.getLongitude().subtract(user1.getLongitude()).doubleValue()));
		double lat1 = Math.toRadians(user1.getLatitude().doubleValue());
		double lat2 = Math.toRadians(user2.getLatitude().doubleValue());

		double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		double d = R * c;
		return d; 
	}

}
